package com.yycx.module.bus.provider.handler.process.form;

import com.yycx.bpm.provider.handler.TaskCompleteHandler;
import com.yycx.bpm.provider.model.TaskRequest;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取任务表单数据
 *
 * @author zyf
 */
@Component
public class PayFormTaskDataHandler implements TaskCompleteHandler {

    @Autowired
    private BusPaymentService paymentService;

    @Autowired
    private BusSupplierBankService supplierBankService;

    @Autowired
    private BusSupplierService supplierService;

    @Autowired
    private BusInvoiceunitBankService invoiceunitBankService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private BusSalemanBankService salemanBankService;
    @Autowired
    private BusSalemanService salemanService;
    @Autowired
    private BusDrugsPlanService busDrugsPlanService;

    @Autowired
    private BusOrderDetailsService detailsService;




    @Override
    public Map<String, Object> initTaskData(ProcessInstance processInstance, Task task, Map<String, Object> variables) {
        Map<String, Object> ret = new HashMap<>(5);
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        if (FlymeUtils.isNotEmpty(payment)) {
            String amountType = payment.getAmountType();

            Long bankId = payment.getBankId();
            Long supplierId = payment.getSupplierId();
            String bankName = "";
            String bankNo = "";
            String userName = "";
            //垫付款
            if (AmountTypeEnum.SupplierBond.getCode().equals(amountType)) {
                BusSupplierBank supplierBank = supplierBankService.getById(bankId);
                BusSupplier supplier = supplierService.getById(supplierId);
                bankName = supplierBank.getBankName();
                bankNo = supplierBank.getBankNo();
                userName = supplier.getSupplierName();
            }
            //开票税金
            if (AmountTypeEnum.Invoice.getCode().equals(amountType)) {
                BusInvoiceunitBank invoiceunitBank = invoiceunitBankService.getById(bankId);
                Long invoiceUnitId = payment.getInvoiceUnitId();
                BusInvoiceunit invoiceunit = invoiceunitService.getById(invoiceUnitId);
                bankName = invoiceunitBank.getBankName();
                bankNo = invoiceunitBank.getBankNo();
                userName = invoiceunit.getInvoiceunitName();
            }
            //市场结算或者退市场保证金
            if (AmountTypeEnum.Settle.getCode().equals(amountType) || AmountTypeEnum.CbReturn.getCode().equals(amountType)) {
                BusSalemanBank salemanBank = salemanBankService.getById(bankId);
                Long saleManId = salemanBank.getSaleManId();
                BusSaleman saleman = salemanService.getById(saleManId);
                bankName = salemanBank.getBankName();
                bankNo = salemanBank.getBankNo();
                userName = saleman.getSaleManName();
            }
            //药品采购
            if (AmountTypeEnum.DrugsPlan.getCode().equals(amountType)) {
                String busDrugsPlanId = payment.getOrderId();
                BusDrugsPlan busDrugsPlan = busDrugsPlanService.getById(busDrugsPlanId);
                userName = busDrugsPlan.getReceivingUnit();
                bankName = busDrugsPlan.getBank();
                bankNo = busDrugsPlan.getBankNo();
            }
            ret.put("bankName", bankName);
            ret.put("bankNo", bankNo);
            ret.put("paymentId", payment.getPaymentId());
            ret.put("userName", userName);
            ret.put("payAmount", payment.getAmount());

        }
        return ret;
    }

    /**
     * 表单提交后业务处理
     *
     * @param taskRequest
     * @param processInstance
     * @param task
     * @param completeVariables
     */
    @Override
    public void completeAfter(TaskRequest taskRequest, ProcessInstance processInstance, Task task, Map<String, Object> completeVariables, OpenUser user) {
        String businessKey = processInstance.getBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        String amountType = payment.getAmountType();
        Object payBankId = completeVariables.get("payBankId");
        Object payDate = completeVariables.get("payDate");
        if (FlymeUtils.isNotEmpty(payment)) {
            payment.setPayBankId(Long.parseLong(payBankId.toString()));
            payment.setPayDate(DateUtils.strToDate(payDate.toString()));
            payment.setPayUserId(user.getUserId());
            payment.setPaymentState(CommonConstants.INT_2);
            //结算
            if (AmountTypeEnum.Settle.getCode().equals(amountType)) {
                String orderDetailsId = payment.getOrderDetailsId();
                BusOrderDetails orderDetails = detailsService.getById(orderDetailsId);
                //应结算金额
                BigDecimal amount = payment.getAmount();
                Integer settleType=FlymeUtils.getInteger(payment.getSettleType(),1);

                BigDecimal settleAmount2=FlymeUtils.getBigDecimal(orderDetails.getSettleAmount2(),"0");

                BigDecimal test=amount.subtract(settleAmount2);
                if(FlymeUtils.isNotEmpty(settleAmount2)&&FlymeUtils.eqzero(test)){
                  //  settleType=2;
                }
                payment.setPaymentState(CommonConstants.INT_2);
                //更新结算信息
                detailsService.updateJiesuanInfo(payment, amount,settleType);
            }
            paymentService.updateById(payment);
        }
        
        //采购业务处理
        if (AmountTypeEnum.DrugsPlan.getCode().equals(amountType)) {
            String busDrugsPlanId = payment.getOrderId();
            BusDrugsPlan busDrugsPlan = busDrugsPlanService.getById(busDrugsPlanId);
            Integer checkState = busDrugsPlan.getCheckState();
            //预付款审核结束
            if (checkState == 3) {
                busDrugsPlan.setCheckState(4);
            }
            //差额付款审核结束
            if (checkState == 5) {
                busDrugsPlan.setCheckState(6);
            }
            //全额付款审核结束
            if (checkState == 7) {
                busDrugsPlan.setCheckState(8);
            }
            busDrugsPlanService.updateById(busDrugsPlan);
        }
    }

}
